package Jogo.Comm {
	import Jogo.Entities.Tile;
	import Jogo.Entities.Turret;
	import Jogo.Entities.TurretPoint;
	import Jogo.States.PhaseState;
	
	import flash.geom.Point;
	import flash.net.dns.PTRRecord;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	public class MsgHandler	{
		/* Defines the types of the messages */
		public static const CONNECT : int = 0;
		public static const DISCONNECT : int = 1;
		public static const ENTITY_REDEFINITION : int = 2;
		public static const ENTITY_UPDATE : int = 3;
		public static const GAME_STATE : int = 4;
		public static const ACK : int = 5;
		public static const ERROR : int = 6;
		
		//The action the types COMM_MANAGEMENT, ENTITY_UPDATE and GAME_STATE demand
		public static const CREATE : int = 0;
		public static const SELL : int = 1;
		public static const UPGRADE : int = 2;
		public static const START : int = 3;
		public static const WIN : int = 4;
		public static const LOSE : int = 5;
		
		/* The msg string */
		private static var msg : Message;
		
		
		/** Handle a message receive from the socket 
		 * 
		 * @param msg The message read from the socket 
		 */
		public static function receiveMessage(byteA : ByteArray) : void {
			msg = new Message();
			msg.decode(new String(byteA));
			
			//trace("Message received:\n"+msg.getMsg());
			
			switch(msg.getType()){
				case CONNECT:
					//add player com o ip;
					PhaseState.addPlayer(msg.getIP());
					break;
				
				case (DISCONNECT):
					PhaseState.removePlayer(msg.getIP());
					break;					
				
				case ENTITY_UPDATE:
					if (msg.getAction() == CREATE){
						PhaseState.createTurret(msg.getTurret(), msg.getPoint(), msg.getSender());
					} else if (msg.getAction() == UPGRADE){
						PhaseState.upgradeTurret(msg.getPoint().x, msg.getPoint().y, msg.getSender());
					} else if (msg.getAction() == SELL){
						PhaseState.sellTurret(msg.getPoint().x, msg.getPoint().y, msg.getSender());
					}
					break;
			}
		}
		
		public static function createErrorMessage(receiver:String, error:String): void{
			var msg : Message = new Message();
			msg.createErrorMessage(error, receiver);

			FlashSocket.send(msg.getMsg());
		}
		
		public static function createEntityUpdateMessage(action:int, x:int, y:int, 
														 turret:int, receivers:Vector.<String>):void{
			for (var i:int = 1; i<receivers.length; i++){
				var msg : Message = new Message();
				msg.createEntityUpdateMessage(action, x, y, receivers[i], turret);
				
				FlashSocket.send(msg.getMsg());
			}
		}
		
		public static function createEntityRedefinitionMessage(pla:Vector.<int>, 
															   pts:Vector.<Point>, entityType:Vector.<int>,
															   entityLevel:Vector.<int>, players:Vector.<String>) : void {
			
			for(var i:int = 1; i<players.length; i++){
				var msg : Message = new Message();
				msg.createEntityRedefinitionMessage(pla, pts, entityType, entityLevel, i, players[i]);
				
				trace("Enviando redefinition:\n"+msg.getMsg());
				FlashSocket.send(msg.getMsg());
			}
		}
		
		public static function createGameStateMessage(action:int, players:Vector.<String>):void{
			for(var i:int = 1; i<players.length; i++){
				var msg : Message = new Message();
				msg.createGameStateMessage(action, players[i]);
				
				trace("Enviando Gamestate:\n"+msg.getMsg());
				FlashSocket.send(msg.getMsg());
			}
		}		
	}
}